<?php
/*
 * @开发工具: JetBrains PhpStorm.
 * @文件名：User.class.php
 * @类功能: 用户控制器
 * @开发者: 陈旭林
 * @开发时间： 2017-09-01
 * @版本：version 1.0
 */

namespace app\partner\controller;

use app\common\controller\PartnerBaseController;
use app\common\enum\CompanyStatus;
use app\common\enum\IsCompany;
use app\common\enum\IsHistory;
use app\common\ext\IDb;
use app\common\ext\IRequest;
use app\common\ext\User;

class UserCompany extends PartnerBaseController {

    /**
     * @功能：个人公司信息
     * @开发者：cxl
     */
    public function company_info(){
        //接收参数
        $user_id = self::get_data('user_id',self::NOT_EMPTY,'客户ID不能为空');

        //检查用户所属合作商
        $result = self::check_user_belong($user_id);
        if($result !== true){
            self::set_code(self::WARNING_0001);
            self::set_msg($result);
            self::send();
        }

        //判断公司是否存在
        $user_company_where['is_history'] = IsHistory::not;
        $user_company_where['user_id'] = $user_id;
        $user_company_info = IDb::getInstance("asset_company")->setDbWhere($user_company_where)->row();
        if(empty($user_company_info) || !is_array($user_company_info)){
            $user_company_info = array();
        }

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("company_info",$user_company_info);
        self::send();
    }

    /**
     * @功能：编辑个人公司
     * @开发者：cxl
     */
    public function company_edit(){
        //接收参数
        $user_id = self::get_data('user_id',self::NOT_EMPTY,'客户ID不能为空');
        $company_name  = self::get_data('company_name',IRequest::NOT_EMPTY,"公司名称不能为空！");
        $law_man  = self::get_data('law_man',IRequest::NOT_EMPTY,"企业法人不能为空！");
//        $licence_num  = self::get_data('licence_num');
        $manager  = self::get_data('manager');
        $phone  = self::get_data('phone',IRequest::NOT_EMPTY,"联系方式不能为空！");
//        $tel  = self::get_data('tel');
        $company_type  = self::get_data('company_type');
        $register_capital  = self::get_data('register_capital',IRequest::NOT_EMPTY,"注册资金不能为空！");
        $register_date  = self::get_data('register_date');
        $hold_ratio  = self::get_data('hold_ratio',IRequest::NOT_EMPTY,"申请人持股比例不能为空！");
        $employee_num  = self::get_data('employee_num',IRequest::NOT_EMPTY,"员工人数不能为空！");
        $flow_out  = self::get_data('flow_out',IRequest::NOT_EMPTY,"月均对公流水不能为空！");
        $flow_in  = self::get_data('flow_in',IRequest::NOT_EMPTY,"月均对私流水不能为空！");
        $website_url  = self::get_data('website_url');
        $license_address  = self::get_data('license_address');
        $office_address  = self::get_data('office_address');
        $licence_url  = self::get_data('licence_url');

        //长度限制
        $register_capital = self::data_filter($register_capital,self::DOUBLE,'注册资本');
        $hold_ratio = self::data_filter($hold_ratio,self::DOUBLE,'持股比例');
        $employee_num = self::data_filter($employee_num,self::INT,'员工人数');
        $flow_out = self::data_filter($flow_out,self::DOUBLE,'月均对公流水');
        $flow_in = self::data_filter($flow_in,self::DOUBLE,'月均对私流水');

        //判断是否设置空值
        if(trim($register_date) == "") $register_date = null;

        //判断公司类型
        if(empty($company_type)){
            $company_type = 1;
        }

        //取得用户最新公司信息
        $user_company_where = null;
        $user_company_where['user_id'] = $user_id;
        $user_company_where['company_status'] = array("neq",CompanyStatus::OK);
        $user_company_where['is_history'] = IsHistory::not;
        $user_company_info = IDb::getInstance("asset_company")->setDbWhere($user_company_where)->row();
        if($user_company_info === false){
            self::set_code(self::WARNING_0001);
            self::set_msg("编辑个人公司信息失败！");
            self::send();
        }

        $user_company_data = null;
        $user_company_data['user_id'] = $user_id;
        $user_company_data['company_name'] = $company_name;
        $user_company_data['law_man'] = $law_man;
        $user_company_data['manager'] = $manager;
        $user_company_data['phone'] = $phone;
//        $user_company_data['tel'] = $tel;
        $user_company_data['register_capital'] = $register_capital;
        $user_company_data['register_date'] = $register_date;
        $user_company_data['company_type'] = $company_type;
        $user_company_data['hold_ratio'] = $hold_ratio;
        $user_company_data['employee_num'] = $employee_num;
        $user_company_data['flow_out'] = $flow_out;
        $user_company_data['flow_in'] = $flow_in;
//        $user_company_data['licence_num'] = $licence_num;
        $user_company_data['website_url'] = $website_url;
        $user_company_data['license_address'] = $license_address;
        $user_company_data['office_address'] = $office_address;
        $user_company_data['licence_url'] = $licence_url;
        $user_company_data['company_status'] = CompanyStatus::Wait;
        $user_company_data['is_history'] = IsHistory::not;

        //检查用户所属合作商
        $result = self::check_user_belong($user_id);
        if($result !== true){
            self::set_code(self::WARNING_0001);
            self::set_msg($result);
            self::send();
        }

        //开启事物处理
        IDb::dbStartTrans();

        //判断是否添加
        if(empty($user_company_info['user_company_id'])){
            //修改历史状态
            $user_company_where = null;
            $user_company_where['user_id'] = $user_id;
            $company_update_data['is_history'] = IsHistory::yes;
            $user_company_upd = IDb::getInstance("asset_company")->setDbWhere($user_company_where)->setDbData($user_company_data)->upd();
            if($user_company_upd === false){
                //数据回滚
                IDb::dbRollback();

                //返回错误
                self::set_code(self::WARNING_0002);
                self::set_msg("编辑个人公司信息失败！");
                self::send();
            }

            //添加个人公司信息
            $user_company_data['create_time'] = get_date_time();
            $user_company_add = IDb::getInstance("asset_company")->setDbData($user_company_data)->add();
            if($user_company_add === false){
                //数据回滚
                IDb::dbRollback();

                //返回错误
                self::set_code(self::WARNING_0003);
                self::set_msg("编辑个人公司信息失败！");
                self::send();
            }
        }else{
            //未通过 || 审核失败 修改公司信息
            $user_company_where = null;
            $user_company_data['update_time'] = get_date_time();
            $user_company_where['user_company_id'] = $user_company_info['user_company_id'];
            $user_company_add = IDb::getInstance("asset_company")->setDbWhere($user_company_where)->setDbData($user_company_data)->upd();
            if($user_company_add === false){
                //数据回滚
                IDb::dbRollback();

                //返回错误
                self::set_code(self::WARNING_0004);
                self::set_msg("编辑个人公司信息失败！");
                self::send();
            }
        }

        //同步个人信息
        $result = User::syncMaterialToUserInfo($user_id,User::MATERIAL_COMPANY,IsCompany::Yes);
        if($result === false){
            IDb::dbRollback();
            self::set_code(self::WARNING_0005);
            self::set_msg("编辑个人公司信息失败！");
            self::send();
        }

        //事物提交
        IDb::dbCommit();

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::send();
    }
}
